/* Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.snakerflow.framework.ws.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.snaker.engine.access.Page;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.Surrogate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.snakerflow.framework.flow.service.SnakerEngineFacets;
import com.snakerflow.framework.ws.Constant;
import com.snakerflow.framework.ws.vo.WsCommonResponse;

/**
 * Snaker流程引擎委托授权相关Controller
 * @author 何东
 * @since 0.1
 */
@Controller
@RequestMapping(value = "/ws/snaker/surrogate")
public class WsSurrogateController {
	
	@Autowired
	private SnakerEngineFacets facets;
	
	/**
	 * 根据流程名称分页查询委托管理列表
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public WsCommonResponse<Surrogate> list(HttpServletRequest request) {
		WsCommonResponse<Surrogate> wsResp = new WsCommonResponse<Surrogate>();
		
		int pageSize = 0;
		if (StringUtils.isNotBlank(request.getParameter(Constant.PARA_PAGE_SIZE))) {
			pageSize = Integer.valueOf(request.getParameter(Constant.PARA_PAGE_SIZE));
		}
		
		Page<Surrogate> page = null;
		if (pageSize > 0) {
			page = new Page<Surrogate>(pageSize);
		} else {
			page = new Page<Surrogate>();
		}
		
		int currPage = 1;
		if (pageSize > 0 && StringUtils.isNotBlank(request.getParameter(Constant.PARA_START_IDX))) {
			currPage = Integer.valueOf(request.getParameter(Constant.PARA_START_IDX)) / pageSize + 1;
		}
		
		page.setPageNo(currPage);
		
		facets.searchSurrogate(page, StringUtils.isNotBlank(request.getParameter(Constant.PARA_PROCESSNAME)) ?
				new QueryFilter().setNames(new String[]{request.getParameter(Constant.PARA_PROCESSNAME)}) :
				new QueryFilter());
		
		wsResp.setPage(page);
		return wsResp;
	}
	
	/**
	 * 获取所有流程名称
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "allProcessNames", method = RequestMethod.GET)
	public WsCommonResponse<String> allProcessNames(Model model) {
		WsCommonResponse<String> wsResp = new WsCommonResponse<String>();
		wsResp.setList(facets.getAllProcessNames());
		return wsResp;
	}

	/**
	 * 根据ID查询委托授权信息
	 * @param id
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "view/{id}", method = RequestMethod.GET)
	public WsCommonResponse<Surrogate> view(@PathVariable("id") String id, Model model) {
		WsCommonResponse<Surrogate> wsResp = new WsCommonResponse<Surrogate>();
		wsResp.setObj(facets.getSurrogate(id));
		return wsResp;
	}
	
	/**
	 * 维护委托授权
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "update")
	public WsCommonResponse update(HttpServletRequest request) {
		WsCommonResponse wsResp = new WsCommonResponse();
		
		Surrogate surrogate = JSON.parseObject(request.getParameter(Constant.PARA_SURROGATE_VO), new TypeReference<Surrogate>(){});
		if (surrogate != null) {
			surrogate.setSdate(surrogate.getSdate() + " 00:00:00");
			surrogate.setEdate(surrogate.getEdate() + " 23:59:59");
			facets.addSurrogate(surrogate);
			wsResp.setMsg("维护委托授权成功.");
		} else {
			wsResp.setSuccess(false);
			wsResp.setMsg("维护委托授权失败.");
		}
		
		return wsResp;
	}
	
	/**
	 * 删除委托授权
	 * @param id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "delete/{id}")
	public WsCommonResponse delete(@PathVariable("id") String id) {
		WsCommonResponse wsResp = new WsCommonResponse();
		
		facets.deleteSurrogate(id);
		
		wsResp.setMsg("删除委托授权成功.");
		return wsResp;
	}
}
